﻿using System;
using System.Data.SqlClient;
using System.Threading;
using System.Windows.Forms;
using com.fukefu.sql;

namespace com.fukefu.template
{
    public partial class ListForm : BaseForm
    {
        private static ListForm rf;
        private static Object obj = new Object();

        public static ListForm GetInstance(string tableName)
        {
            if (rf == null || rf.IsDisposed)
            {
                lock (obj)
                {
                    if (rf == null || rf.IsDisposed)
                    {
                        rf = new ListForm(tableName);
                    }
                }
            }
            return rf;
        }
        string tableName;
        SqlServerOperate sql = new SqlServerOperate();

        private ListForm(string tableName)
        {
            InitializeComponent();
            this.Text = tableName + "列表";
            this.tableName = tableName;
            //分类
            comboBox1.Items.Add("所有");
            comboBox1.SelectedIndex = 0;
            new Thread(new ThreadStart(Init)).Start();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            FlushListView();
        }
        private void Init()
        {
            SqlDataReader re = sql.Query("select distinct 分类 from [所有" + tableName + "]");
            while (re.Read())
            {
                SetControl("combobox1", ControlValue.Combobox, re["分类"]);
            }
            re.Close();
            SetControl("button1", ControlValue.Enable, true);
            ContextMenuStrip cms = new ContextMenuStrip();
            ToolStripItem tsi = new ToolStripMenuItem("修改");
            tsi.Click += delegate
            {
                ShowEditForm();
            };
            cms.Items.Add(tsi);
            tsi = new ToolStripMenuItem("删除选中");
            tsi.Click += delegate
            {
                if (listView1.SelectedItems.Count > 0 && ShowCheckDialog("确认删除吗?"))
                {
                    sql.Update("delete from [_mb_" + tableName + "] where id=" + Convert.ToInt32(listView1.SelectedItems[0].SubItems[0].Text));
                    FlushListView();
                }
            };
            cms.Items.Add(tsi);
            listView1.ContextMenuStrip = cms;
        }
        //显示编辑窗口
        private void ShowEditForm()
        {
            if (listView1.SelectedItems.Count > 0)
            {
                if (tableName.Equals("模板"))
                {
                    ShowForm(new TemplateMainForm(listView1.SelectedItems[0].SubItems[0].Text));
                }
                else if (tableName.Equals("标签"))
                {
                    ShowForm(new LabelForm(listView1.SelectedItems[0].SubItems[0].Text));
                }
            }
        }
        private void FlushListView()
        {
            listView1.Items.Clear();
            new Thread(new ThreadStart(ShowListView)).Start();
        }
        //显示数据
        private void ShowListView()
        {
            string d1 = GetControl("dtpk1", ControlValue.DateTimePicker).ToString();
            string d2 = GetControl("dtpk2", ControlValue.DateTimePicker).ToString();
            string type = GetControl("comboBox1", ControlValue.Text).ToString();
            string cmd = "select [编号],[审核状态],[时间],[分类],[描述] from [所有" + tableName + "] where uid='" + LoginUser.Uid + "' and ";
            if (!type.Equals("所有"))
            {
                cmd += "[分类]='" + type + "' and ";
            }
            cmd += "datediff(d,[时间],'" + d1 + "')<=0 and datediff(d,[时间],'" + d2 + "')>=0 order by [审核状态] asc,[时间] desc";

            this.CreateListView(ref sql, "listView1", cmd);

            SetControl("button1", ControlValue.Enable, true);
        }

        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            ShowEditForm();
        }
    }
}
